package ltd.newbee.mall.api.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import ltd.newbee.mall.api.admin.param.BatchIdParam;
import ltd.newbee.mall.api.admin.param.ShopsAddParam;
import ltd.newbee.mall.api.admin.param.VideosAddParam;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.entity.Shop;
import ltd.newbee.mall.entity.Video;
import ltd.newbee.mall.service.ShopService;
import ltd.newbee.mall.service.VideoService;
import ltd.newbee.mall.util.BeanUtil;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Zcq
 * @create: 2022-09-14 20:02
 **/

@RestController
@Api(value = "v1", tags = "8-8.后台管理系统商店模块接口")
@RequestMapping("/manage-api/v1")
public class ShopAPI {
    @Autowired
    ShopService shopService;

    @RequestMapping(value = "/shops", method = RequestMethod.POST)
    @ApiOperation(value = "添加商店", notes = "添加商店")
    public Result addVideo(@RequestBody @Valid ShopsAddParam shopsAddParam){
        Shop shop = new Shop();
        BeanUtil.copyProperties(shopsAddParam,shop);
        String result = shopService.addShop(shop);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }
    @RequestMapping(value = "/shops", method = RequestMethod.PUT)
    @ApiOperation(value = "修改商店信息", notes = "修改商店信息")
    public Result updateShop(@RequestBody Shop shop){
        String result = shopService.upadateShop(shop);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }
    @RequestMapping(value = "/shopGoods", method = RequestMethod.GET)
    @ApiOperation(value = "查询商店商品", notes = "查询商店商品")
    public Result FindGoodsByShopId(@RequestParam @ApiParam(value = "商店Id") Integer shopId){
        List<NewBeeMallGoods> result= shopService.FindGoodsByShopId(shopId);
        return ResultGenerator.genSuccessResult(result);
    }
    @RequestMapping(value = "/shops", method = RequestMethod.GET)
    @ApiOperation(value = "商店列表", notes = "商店列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "页码") Integer pageNumber,
                       @RequestParam(required = false) @ApiParam(value = "每页条数") Integer pageSize) {
        if (pageNumber == null || pageNumber < 1 || pageSize == null || pageSize < 10) {
            return ResultGenerator.genFailResult("分页参数异常！");
        }
        Map params = new HashMap(4);
        params.put("page", pageNumber);
        params.put("limit", pageSize);
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(shopService.getShopPage(pageUtil));
    }
}
